<?php
if (!defined('HAPPY')) exit("Access Denied");
/**************************
@Author  : sulwan
@Content : 数据库选择类
**************************/
class Happy_db {
	private $db;
	private $type;
	//构造函数
	public function __construct() {
		global $config;
		$this->type = $config['type'];
		if(empty($this->type)) {
            echo '数据库类型为空';
			exit;
		}
		$typeDb = 'Happy_'.$config['type'];
		$this->db = new  $typeDb($config);
	}

	//获取所有数据
	public function getAll($sql) {
		global $config;
		$this->type = $config['type'];
		if(empty($this->type)) {
            echo '数据库类型为空';
			exit;
		}
		$p = $this->type.'_db_query_sql';
    	if ( !empty($sql) ){
			$result = $this->db->$p($sql);
    	}
        if ( !$result ) {
            return false;
        }
		$p2 = $config[type].'_db_fetch_all';
		return $this->db->$p2();
	}

	//获取所有数据
	public function getAll_Num($sql) {
		global $config;
		$this->type = $config['type'];
		if(empty($this->type)) {
            echo '数据库类型为空';
			exit;
		}
		$p = $this->type.'_db_query_sql';
    	if ( !empty($sql) ){
			$result = $this->db->$p($sql);
    	}
        if ( !$result ) {
            return false;
        }
		$p2 = $config[type].'_db_fetch_all_num';
		return $this->db->$p2();
	}

	//获取单条数据
	public function getRow($sql){
		global $config;
		$this->type = $config['type'];
		if(empty($this->type)) {
            echo '数据库类型为空';
			exit;
		}
		$p = $this->type.'_db_query_sql';
    	if ( !empty($sql) ){
			$result = $this->db->$p($sql);
    	}
        if ( !$result ) {
            return false;
        }
		$p2 = $this->type.'_db_fetch_assoc';
		return $this->db->$p2();
	}

	//获取单列
	public function getOne($sql){
		global $config;
		$this->type = $config['type'];
		if(empty($this->type)) {
            echo '数据库类型为空';
			exit;
		}
		$p = $this->type.'_db_query_sql';
    	if ( !empty($sql) ){
			$result = $this->db->$p($sql);
    	}
        if ( !$result ) {
            return false;
        }
		$p2 = $this->type.'_db_fetch_assoc';
		return @current($this->db->$p2());
	}

	//执行语句
	public function execute($sql){
		global $config;
		$this->type = $config['type'];
		if(empty($this->type)) {
            echo '数据库类型为空';
			exit;
		}
		$p = $this->type.'_db_query_sql';
    	if ( !empty($sql) ){
			$result = $this->db->$p($sql);
    	}
        if ( !$result ) {
            return false;
        }
		$p2 = $config['type'].'_db_insert_id';
		return $this->db->$p2();
	}

	//获取数据库版本
	public function version(){
		return $this->db->version();
	}

	//获取资源
	public function getFetch($sql){
		global $config;
		$this->type = $config['type'];
		if(empty($this->type)) {
            echo '数据库类型为空';
			exit;
		}
		$p = $this->type.'_db_query_sql';
    	if ( !empty($sql) ){
			$result = $this->db->$p($sql);
    	}
        if ( !$result ) {
            return false;
        }
		$p2 = $config['type'].'_db_fetch_row';
		return $this->db->$p2();
	}

	//获取数据库版本
	public function num_rows($sql){
		global $config;
		$this->type = $config['type'];
		if(empty($this->type)) {
            echo '数据库类型为空';
			exit;
		}
		$p = $this->type.'_db_query_sql';
    	if ( !empty($sql) ){
			$result = $this->db->$p($sql);
    	}
        if ( !$result ) {
            return false;
        }
		return $this->db->num_rows();
	}

	//获取数据库版本
	public function num_fields($sql){
		global $config;
		$this->type = $config['type'];
		if(empty($this->type)) {
            echo '数据库类型为空';
			exit;
		}
		$p = $this->type.'_db_query_sql';
    	if ( !empty($sql) ){
			$result = $this->db->$p($sql);
    	}
        if ( !$result ) {
            return false;
        }
		return $this->db->num_fields();
	}
}
?>